package com.zzq.my.jetpacks.jectpackapp.intercept.impl

import android.util.Log
import com.zzq.my.jetpacks.jectpackapp.intercept.BaseInterceptImpl
import com.zzq.my.jetpacks.jectpackapp.intercept.InterceptChain
import com.zzq.my.jetpacks.jectpackapp.net.ApiUrl
import com.zzq.my.jetpacks.jectpackapp.view.FangIOSDialog
import com.zzq.my.jetpacks.net.rxhttp.errorMsg
import com.zzq.my.jetpacks.net.rxhttp.show
import com.zzq.my.jetpacks.util.rxLaunch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import rxhttp.RxHttp
import rxhttp.toStr
import rxhttp.tryAwait

/*
  * ================================================
  * 描述：完善个人资料信息弹窗
  * 作者：zhuangzeqin
  * 时间: 2023/6/9 14:40
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
class InterceptFillInfo(private val bean: JobInterceptBean) : BaseInterceptImpl() {
    override fun intercept(chain: InterceptChain) {
        super.intercept(chain)
        if (!bean.isFillInfo) {//条件满足进件拦截
            //拦截
            //跳转新页面
            showDialogTips(chain)
        } else {
            //放行- 转交给下一个拦截器
            chain.process()
        }
    }

    private fun showDialogTips(chain: InterceptChain) {
        FangIOSDialog(chain.activity).apply {
            setTitle("完善信息")
            setMessage("你没有完善信息，你要去完善信息")
            setNegativeButton("跳过") {
                dismiss()
                chain.process()
            }
            setPositiveButton("去完善") {
                "去完善".show()
                //获取数据
                getData()

            }
            show()
        }
    }

    private fun getData()
    {
       val scope =  CoroutineScope(Dispatchers.IO).rxLaunch<String> {
            onRequest = {
                RxHttp.postJson(ApiUrl.FINDBANNER).toStr().tryAwait { it.errorMsg.show() }
            }
            onSuccess = {
                it.show()
            }
            //封装了协程的异常捕获
            onError = {
                Log.d("InterceptFillInfo", "getData: ${it.errorMsg}")
            }
        }
    }
}